package com.xt.purchasingsystem.service.impl.stockManagementServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.SGatherDto;
import com.xt.purchasingsystem.dto.SPayDto;
import com.xt.purchasingsystem.mapper.*;
import com.xt.purchasingsystem.model.MProcedureModule;
import com.xt.purchasingsystem.model.SCell;
import com.xt.purchasingsystem.model.SGatherDetails;
import com.xt.purchasingsystem.model.SPayDetails;
import com.xt.purchasingsystem.service.stockManagementService.InAndOutWarehouseManageService;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 老姚
 * Date:  2020/5/28 11:26
 */
@Service
@Transactional
public class InAndOutWarehouseManageServiceImpl implements InAndOutWarehouseManageService {
    @Autowired
    private SGatherMapper sGatherMapper;
    @Autowired
    private SCellMapper sCellMapper;
    @Autowired
    private SGatherDetailsMapper sGatherDetailsMapper;
    @Autowired
    private SPayMapper sPayMapper;
    @Autowired
    private SPayDetailsMapper sPayDetailsMapper;
    @Autowired
    private MProcedureModuleMapper mProcedureModuleMapper;

    @Override
    public Result getAllIsCheckSgatherByPage(SGatherDto sGatherDto) {
        PageHelper.startPage(sGatherDto.getCurrNo(), sGatherDto.getPageSize());
        List<SGatherDto> allIsCheckSgather = sGatherMapper.getAllIsCheckSgather(sGatherDto);
        PageInfo<SGatherDto> pageInfo = new PageInfo<>(allIsCheckSgather);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Override
    public Result getScellByProductId(String productId) {
        SCell sCell = sCellMapper.getByProductId(productId);
        return ResultFactory.buildSuccessResult("查询成功", sCell);
    }

    @Override
    public Result operationDispatching(SGatherDto sGatherDto) {

        List<SGatherDetails> allSGatherDetailsBySGatherId = sGatherDetailsMapper.getAllSGatherDetailsBySGatherId(sGatherDto.getSgather().getId());
        List<SGatherDetails> collect = allSGatherDetailsBySGatherId.stream().filter(s -> s.getGatherTag().equals("K002-1")).collect(Collectors.toList());
        if (collect!=null&&collect.size()>1){
            System.out.println("入库详细>=2 个没有调度");

            //入库详细>=2 个没有调度
            sGatherMapper.updateByPrimaryKeySelective(sGatherDto.getSgather());
            for (SGatherDetails sgatherDetail : sGatherDto.getSgatherDetails()) {
                sGatherDetailsMapper.updateByPrimaryKeySelective(sgatherDetail);
                SCell sCell = sCellMapper.getByProductId(sgatherDetail.getProductId());
                double v = sCell.getAmount().doubleValue();
                double v1 = sgatherDetail.getGatheredAmount().doubleValue();
                sCell.setAmount(BigDecimal.valueOf(v+v1));
                sCellMapper.updateByPrimaryKeySelective(sCell);
            }

            return ResultFactory.buildSuccessResult("调度成功",false);

        }else {
            System.out.println("有一个入库详细没有调度");
            //只有一个入库详细没有调度
            sGatherDto.getSgather().setStoreTag("K002-2");
            sGatherMapper.updateByPrimaryKeySelective(sGatherDto.getSgather());
            for (SGatherDetails sgatherDetail : sGatherDto.getSgatherDetails()) {
                sGatherDetailsMapper.updateByPrimaryKeySelective(sgatherDetail);
                SCell sCell = sCellMapper.getByProductId(sgatherDetail.getProductId());
                double v = sCell.getAmount().doubleValue();
                double v1 = sgatherDetail.getGatheredAmount().doubleValue();
                sCell.setAmount(BigDecimal.valueOf(v+v1));
                sCellMapper.updateByPrimaryKeySelective(sCell);
            }

            return ResultFactory.buildSuccessResult("调度成功",true);
        }

    }

    @Override
    public Result getAllIsCheckSPayByPage(SPayDto sPayDto) {
        PageHelper.startPage(sPayDto.getCurrNo(), sPayDto.getPageSize());
        List<SPayDto> allIsCheckSPayByPage = sPayMapper.getAllIsCheckSPayByPage(sPayDto);
        PageInfo<SPayDto> pageInfo = new PageInfo<>(allIsCheckSPayByPage);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Override
    public Result deliverySchedule(SPayDto sPayDto) {
        List<SPayDetails> allSGatherDetailsBySGatherId = sPayDetailsMapper.getAllSpayDetailsBySpayId(sPayDto.getSpay().getId());
        List<SPayDetails> collect = allSGatherDetailsBySGatherId.stream().filter(s -> s.getPayTag().equals("K002-1")).collect(Collectors.toList());
        if (collect!=null&&collect.size()>1){
            if (sPayDto.getSpay().getReason().equals("C002-1")){
                String reasonexact = sPayDto.getSpay().getReasonexact();
                String manufactureId = reasonexact.substring(0, reasonexact.lastIndexOf("-"));
                String procedureName = reasonexact.substring(reasonexact.lastIndexOf("-")+1);
                SPayDetails sPayDetails1 = sPayDto.getSpayDetailsList().get(0);
                String productId = sPayDetails1.getProductId();
                MProcedureModule mProcedureModule = mProcedureModuleMapper.getMProcedureModuleByDeliverySchedule(manufactureId, procedureName, productId);
                mProcedureModule.setRenewAmount(sPayDetails1.getPaidAmount());
                mProcedureModuleMapper.updateByPrimaryKeySelective(mProcedureModule);
            }
                sPayMapper.updateByPrimaryKeySelective(sPayDto.getSpay());
                for (SPayDetails sPayDetails : sPayDto.getSpayDetailsList()) {

                    sPayDetailsMapper.updateByPrimaryKeySelective(sPayDetails);
                    SCell sCell = sCellMapper.getByProductId(sPayDetails.getProductId());
                    double v = sCell.getAmount().doubleValue();
                    double v1 = sPayDetails.getPaidAmount().doubleValue();
                    sCell.setAmount(BigDecimal.valueOf(v-v1));
                    sCellMapper.updateByPrimaryKeySelective(sCell);
                }

            return ResultFactory.buildSuccessResult("调度成功",false);

        }else {

            if (sPayDto.getSpay().getReason().equals("C002-1")){
                String reasonexact = sPayDto.getSpay().getReasonexact();
                String manufactureId = reasonexact.substring(0, reasonexact.lastIndexOf("-"));
                String procedureName = reasonexact.substring(reasonexact.lastIndexOf("-")+1);
                SPayDetails sPayDetails1 = sPayDto.getSpayDetailsList().get(0);
                String productId = sPayDetails1.getProductId();
                MProcedureModule mProcedureModule = mProcedureModuleMapper.getMProcedureModuleByDeliverySchedule(manufactureId, procedureName, productId);
                mProcedureModule.setRenewAmount(sPayDetails1.getPaidAmount());
                mProcedureModuleMapper.updateByPrimaryKeySelective(mProcedureModule);
            }
            sPayDto.getSpay().setStoreTag("K002-2");
            sPayMapper.updateByPrimaryKeySelective(sPayDto.getSpay());
            for (SPayDetails sPayDetails : sPayDto.getSpayDetailsList()) {
                sPayDetailsMapper.updateByPrimaryKeySelective(sPayDetails);
                SCell sCell = sCellMapper.getByProductId(sPayDetails.getProductId());
                double v = sCell.getAmount().doubleValue();
                double v1 = sPayDetails.getPaidAmount().doubleValue();
                sCell.setAmount(BigDecimal.valueOf(v-v1));
                sCellMapper.updateByPrimaryKeySelective(sCell);
            }
            return ResultFactory.buildSuccessResult("调度成功",true);
        }
    }
}
